using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using DAL;
using Utility;

public partial class Admin_DoanhNghiepList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }

    }
    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_id_dn.Text != "")
        {
            c.add(Condition.AND, tbDoanhNghiep.tbDoanhNghiepColumns.IDDoanhNghiep, Condition.LIKE,
                txt_id_dn.Text);
        }
        if (txt_dn.Text != "")
        {
            c.add(Condition.AND, tbDoanhNghiep.tbDoanhNghiepColumns.TenDoanhNghiep, Condition.LIKE, txt_dn.Text);
        }
        int total = 0;
        tbDoanhNghiep.tbDoanhNghiepCollection dn_Collection =
            tbDoanhNghiepBus.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
           tbDoanhNghiep.tbDoanhNghiepColumns.IDDoanhNghiep.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = dn_Collection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
            Pager.Visible = false;
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbDoanhNghiep  lsp = tbDoanhNghiepBus .GetByID(id);
                tbDoanhNghiepBus .Delete_DoanhNghiep(id);
            }
        }
        Response.Redirect(Request.RawUrl);

    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {

            tbDoanhNghiep dn_tbl = (tbDoanhNghiep)e.Item.DataItem;
            HyperLink hyp_dn = (HyperLink)e.Item.FindControl("hyp_dn");
            HyperLink hyp_dc = (HyperLink)e.Item.FindControl("hyp_dc");
            HyperLink hyp_dt = (HyperLink)e.Item.FindControl("hyp_dt");
            HyperLink hyp_email = (HyperLink)e.Item.FindControl("hyp_email");
            Literal lit_iddn = (Literal)e.Item.FindControl("lit_iddn");

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");


            hyp_dn.Text = dn_tbl.TenDoanhNghiep;
            hyp_dc.Text = dn_tbl.DiaChi;
            hyp_dt.Text = dn_tbl.DienThoai;
            hyp_email.Text = dn_tbl.Email;
            hyp_dn.NavigateUrl = "DoanhNghiep.aspx?id=" + dn_tbl.IDDoanhNghiep.ToString();

            lit_iddn.Text = dn_tbl.IDDoanhNghiep.ToString();
            hf_id.Value = dn_tbl.IDDoanhNghiep.ToString();

            hpl_Edit.NavigateUrl = "DoanhNghiepUpdate.aspx?id=" + dn_tbl.IDDoanhNghiep.ToString();
        }
    }
}
